//
//  UserLoadViewController.m
//  SunXi
//
//  Created by dllo on 15/12/2.
//  Copyright © 2015年 大远. All rights reserved.
//

#import "UserLoadViewController.h"

@interface UserLoadViewController ()<UITextFieldDelegate>

@end

@implementation UserLoadViewController{
    UILabel *_namelabel;
    UILabel *_passlabel;
    UITextField *_nameField;
    UITextField *_passField;
    UIView *_nameXian;
    UIView *_passXian;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"登陆";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"注册" style:UIBarButtonItemStylePlain target:self action:@selector(registerClick)];
    [self.navigationItem.rightBarButtonItem setTintColor:BUTTONCOLOR];
    SV.BC = [UIColor whiteColor];
//MARK:登陆填写区
    [self initloadValue];

}
/**
 *  登陆信息区
 */
- (void)initloadValue{
    CGFloat hei = HEIGHT * 0.15;
    _namelabel = [[UILabel alloc]init];
    [SV addSubview:_namelabel];
    [_namelabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(SV).offset(40);
        make.top.equalTo(SV).offset(hei);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(40);
    }];
    _namelabel.text = @"账号:";
    
    _nameField = [[UITextField alloc]init];
    [SV addSubview:_nameField];
    [_nameField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_namelabel.mas_right).offset(5);
        make.right.equalTo(SV).offset(-40);
        make.height.mas_equalTo(20);
        make.top.equalTo(_namelabel);
    }];
    _nameField.placeholder = @"请输入账号";
   

    _nameXian = [[UIView alloc]init];
    [SV addSubview:_nameXian];
    _nameXian.BC = BUTTONCOLOR;
    [_nameXian mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(0.5);
        make.top.equalTo(_namelabel.mas_bottom).offset(5);
        make.left.equalTo(_namelabel);
        make.right.equalTo(_nameField);
    }];
    
    
    //密码
    _passlabel = [[UILabel alloc]init];
    [SV addSubview:_passlabel];
    [_passlabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(SV).offset(40);
        make.top.equalTo(_nameXian.mas_bottom).offset(25);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(40);
    }];
    _passlabel.text = @"密码:";
    
    _passField = [[UITextField alloc]init];
    [SV addSubview:_passField];
    [_passField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_passlabel.mas_right).offset(5);
        make.right.equalTo(SV).offset(-40);
        make.height.mas_equalTo(20);
        make.top.equalTo(_passlabel);
    }];
    _passField.secureTextEntry = YES;
    _passField.placeholder = @"请输入密码";
    _passXian = [[UIView alloc]init];
    [SV addSubview:_passXian];
    _passXian.BC = BUTTONCOLOR;
    [_passXian mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(0.5);
        make.top.equalTo(_passlabel.mas_bottom).offset(5);
        make.left.equalTo(_passlabel);
        make.right.equalTo(_passField);
    }];
     _passField.delegate = self;
    _passField.returnKeyType = UIReturnKeyJoin;

    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [SV addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(SV).offset(40);
        make.right.equalTo(SV).offset(-40);
        make.centerY.equalTo(SV);
        make.height.mas_equalTo(30);
    }];
    [button setTitle:@"登陆" forState:UIControlStateNormal];
    button.tintColor = [UIColor whiteColor];
    button.BC = BUTTONCOLOR;
    button.layer.cornerRadius = 5;
    [button addTarget:self action:@selector(loaduserClick) forControlEvents:UIControlEventTouchUpInside];
    
}
- (void)registerClick{
    NSLog(@"注册");
    RegisterViewController *REVC = [[RegisterViewController alloc]init]
    ;
    void(^block)(NSString *,NSString *) = ^(NSString *username,NSString *userKey){
        
    };
    REVC.Block = block;
    [self.navigationController pushViewController:REVC animated:YES];
    
    
    
}
/**
 *  登陆方法
 */
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
        [self loaduserClick];
    return YES;
}
- (void)loaduserClick{
    [ShowMytext showHUDloadView:SV text:@"登陆中..."];
    [[EaseMob sharedInstance].chatManager asyncLoginWithUsername:_nameField.text password:_passField.text completion:^(NSDictionary *loginInfo, EMError *error) {
        [ShowMytext hiddHUDloadView];
        if (!error && loginInfo) {
            [[EaseMob sharedInstance].chatManager setIsAutoLoginEnabled:YES];
            [ShowMytext showAlertViewTitlt:@"恭喜" message:@"登陆成功" selector:self Block:^{
                
                [self saveUser];
            }];
            //设置自动登陆
        }else if ([error.description isEqualToString:@"User do not exist."]){
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"登陆失败" message:@"用户名不存在" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                _passField.text = nil;
                _nameField.text = nil;
            }];
            [alertController addAction:cancelAction];
            [self presentViewController:alertController animated:YES completion:nil];
        }else if ([error.description isEqualToString:@"User name or password is incorrect."]){
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"登陆失败" message:@"用户名或密码错误" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                _passField.text = nil;
            }];
            [alertController addAction:cancelAction];
            [self presentViewController:alertController animated:YES completion:nil];
        }
    } onQueue:nil];
}
/**
 *  登陆成功方法
 */
- (void)saveUser{
    [UserModel shareSingleton].userName = _nameField.text;
    [UserModel shareSingleton].userkey = _passField.text;

//将用户登陆信息存到本地
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    
    [user setObject:[UserModel shareSingleton].userName forKey:@"username"];
    [user setObject:[UserModel shareSingleton].userkey forKey:@"userkey"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"登陆成功" object:_nameField.text];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"已登录" object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];


}


@end
